using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Shumi.UserCacheService.Common.Interface;
using Shumi.UserCacheService.Common;
using Shumi.Fund123.UserModule.Modle;
//using Shumi.UserCacheService.BLL;
using System.Threading;

namespace Shumi.UserCacheService.Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserProxy.Instance.ResetUser("chuanzai");
            this.textBox1.Text = UserProxy.Instance.UserCount.ToString();
            //BLL.UserManager u = new UserManager();
            //u.ResetUser("chuanzai");
            //textBox1.Text = u.UserCount.ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            User u = UserProxy.Instance.GetUser(this.textBox2.Text);
            //UserManager um = new UserManager();
            //User u = um.GetUser(this.textBox2.Text);

            if (u != null)
            {
                MessageBox.Show(u.Name + "," + u.Email);
            }
            else
            {
                MessageBox.Show("no user");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer();
            timer.Interval = 100;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            RunTest(Convert.ToInt32(this.textBox3.Text));
        }

        System.Timers.Timer timer = null;

        private void RunTest(int p)
        {
            for (int i = 0; i < p; i++)
            {
                Thread t = new Thread(GetUser);
                t.Start();
            }
        }

        static int n = 0;

        private void GetUser()
        {
            try
            {
                UserProxy.Instance.GetUser("aaa");
            }
            catch(Exception ex)
            {
                if (n < 10)
                {
                    MessageBox.Show(ex.Message + ex.StackTrace);
                    n++;
                }

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            UserTest ut = new UserTest();
            ut.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serviceController1.Stop();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            serviceController1.Start();
        }
    }
}